// ignore_for_file: file_names

import "package:flutter/material.dart";

class LoginButton extends StatefulWidget {
    final double width;
    final double height;
    final String title;
    final double? borderRadius;
    final VoidCallback? onTap;
    final BorderRadiusGeometry borderRadiusGeometry;
    // final LoginButtonState _state;
    LoginButton({super.key, 
        this.width = 200,
        this.height = 50,
        this.title = "登录",
        this.borderRadius = 1.0,
        this.onTap
    }): borderRadiusGeometry = createBorderRadius(borderRadius: borderRadius ?? 1.0);

    static BorderRadiusGeometry createBorderRadius({double borderRadius = 1.0}) {
        if(borderRadius > 0) {
            return BorderRadius.all(Radius.circular(borderRadius));
        } else {
            return BorderRadius.zero;
        }
    }

    @override
    State<StatefulWidget> createState() {
        return LoginButtonState(
            title: title,
            width: width,
            height: height,
            onTap: onTap,
            borderRadius: borderRadiusGeometry
        );
    }
}

class LoginButtonState extends State<LoginButton> {
    final double width;
    final double height;
    final String title;
    final BorderRadiusGeometry? borderRadius;
    final VoidCallback? onTap;
    // 定义state
    bool showLoading = false;
    bool isPressed = false;
    LoginButtonState({
        this.title = "登录",
        this.width = 200,
        this.height = 50,
        this.borderRadius,
        this.onTap
    }): super();

    Gradient getButtonBackground() {
        if(isPressed) {
            return const LinearGradient(
                colors: [
                    Color.fromARGB(255, 0, 93, 234),
                    Color.fromARGB(255, 0, 197, 251)
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter
            );
        } else {
            return const LinearGradient(
                colors: [
                    Color.fromARGB(255, 51,51,51),
                    Color.fromARGB(255, 51,51,51)
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter
            );
        }
    }
    double get buttonWidth {
        if(width >=100) {
            return width;
        } else {
            return 100.0;
        }
    }
    void onTabPress() {
        setState(() {
            isPressed = true;
        });
    }
    void onTabEnd() {
        setState(() {
            isPressed = false;
        });
        if(onTap is Function) {
            onTap!();
        }
    }
    @override
    Widget build(BuildContext context) {
        double defaultWidth =  buttonWidth;
        double logonButtonWidth = defaultWidth;
        double logonButtonHeight = height > 0 ? height : 50;
        return GestureDetector(
            onTapDown: (TapDownDetails e) => onTabPress(),
            onTapUp: (TapUpDetails e) => onTabEnd(),
            onTapCancel: () => onTabEnd(),
            child: Container(
                width: logonButtonWidth,
                height: logonButtonHeight,
                padding: const EdgeInsets.all(0),
                decoration: BoxDecoration(
                    borderRadius: borderRadius,
                    gradient: getButtonBackground()
                ),
                child: Center(
                    child: !showLoading ? Text(
                        title,
                        overflow: TextOverflow.ellipsis,
                        softWrap: false,
                        style: const TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                        )) : const CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
                    ),
                ),
            )
        );
    }
    @override
    void initState() {
        super.initState();
    }
}
